<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .progress {
      position: relative;
      margin: 100px auto;
      height: 28px;
      width: 498px;
      border: 1px solid red;
      border-radius: 15px;
      overflow: hidden;
    }

    .mask {
      position: absolute;
      height: 100%;
      background: orange;
      border-radius: 15px;
      width: 0;
      width: 100%;
    }

    .mask span {
      position: absolute;
      top: 4px;
      /* left: 10px; */
      height: 20px;
      background: pink;
    }
  </style>
</head>

<body>
  <div class="progress">
    <div class="mask">
      <span></span>
    </div>
  </div>

  <script>
    //需求：使用一个定时器来使用一个进度条
    let progress = document.querySelector(".progress")
    let mask = document.querySelector(".mask")
    let span = document.querySelector("span")

    var w = 0;
    var res = 0;
    var progressW = 0;
    var timer = setInterval(() => {
        w += 20; //每一次增加的值！
        res = `${w}px` //这个是进度条的值（每一次递增后得到的值）
        progressW = progress.offsetWidth //进图条的宽！
        var tip = (`${w}` / progressW * 100).toFixed(1) + "%"; //显示100%的提示
        // console.log(tip);
        span.innerHTML = tip //提示进度条的百分比
        span.style.left = res //
        mask.style.width = res; //宽度

        if (res === (progressW + "px")) { //到达100%，清除定时器
          span.style.left = 440 + "px";
          clearInterval(timer)
        }
        // console.log(res);
        // console.log(progressW + "px");

      },
      500)
  </script>
</body>

</html>